package com.yf.ccdugtfood.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import com.yf.ccdugtfood.R;
import com.yf.ccdugtfood.bean.Food;

import java.util.List;

public class FoodsAdapter extends BaseAdapter {
    private Context mContext;
    private List<Food> foodList;
    private OnSelectListener onSelectListener;

    public FoodsAdapter(Context context,OnSelectListener onSelectListener) {
        this.mContext = context;
        this.onSelectListener = onSelectListener;
    }

    public void setData(List<Food> foodList){
        this.foodList = foodList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return foodList == null ? 0 : foodList.size();
    }

    @Override
    public Food getItem(int position) {
        return foodList == null ? null : foodList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null ){
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.menu_item,null);
            viewHolder.tv_food_name = (TextView) convertView.findViewById(R.id.tv_food_name);
            viewHolder.tv_taste = (TextView)convertView.findViewById(R.id.tv_taste);
            viewHolder.tv_sale_num  = (TextView)convertView.findViewById(R.id.tv_sale_num);
            viewHolder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
            viewHolder.iv_food_pic = (ImageView)convertView.findViewById(R.id.iv_food_pic);
            viewHolder.btn_add_car=(Button) convertView.findViewById(R.id.btn_add_car);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final Food bean = getItem(position);
        if (bean!=null){
            viewHolder.tv_food_name.setText(bean.getFod_name());
            viewHolder.tv_taste.setText(bean.getFod_intr());
            viewHolder.tv_sale_num.setText("月售99");
            viewHolder.tv_price.setText(" ¥"+bean.getFod_price());
            Glide.with(mContext).load(bean.getFod_pic()).error(R.mipmap.ic_launcher).into(viewHolder.iv_food_pic);
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //菜品详情
            }
        });
        //加入购物车
        viewHolder.btn_add_car.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onSelectListener.onSelectAddCar(position);
            }
        });
        return convertView;
    }
    class ViewHolder{
        public TextView tv_food_name,tv_taste,tv_sale_num,tv_price;
        public Button btn_add_car;
        public ImageView iv_food_pic;
    }
    public interface OnSelectListener{
        void onSelectAddCar(int position);
    }
}
